package edu.gtcfla.km.appmodules.web;

import java.util.LinkedHashSet;
import java.util.Set;

import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.vsg.biz.BusinessOperationException;
import org.vsg.common.i18n.I18nMessageService;
import org.vsg.common.web.json.ListResult;
import org.vsg.common.web.json.SubmitResult;
import org.vsg.search.api.criterion.MapPaginaitionCriteria;
import org.vsg.search.api.criterion.PaginationCriteria;
import org.vsg.search.api.criterion.SearchCriteriaCls;
import org.vsg.security.annotation.RegisterRequestPermissionStrategy;

import edu.gtcfla.km.appmodules.service.CategoryService;
import edu.gtcfla.km.appmodules.service.UserGroupService;
import edu.gtcfla.km.appmodules.service.UserPermissionService;
import edu.gtcfla.km.appmodules.vo.UserGroupFormVO;
import edu.gtcfla.km.appmodules.vo.UserGroupListVO;
import edu.gtcfla.km.appmodules.vo.UserGroupMemberFormVO;


@Controller
@Lazy
@RequestMapping(edu.gtcfla.km.core.ApplicationConstant.DATA_RERESOURCE)
public class UserGroupController {
	
	@Autowired(required=false)
	private CategoryService categoryService;
	
	@Autowired(required=false)
	private UserPermissionService userPermissionService;
	
	@Autowired(required=false)
	private UserGroupService userGroupService;
	
	@Autowired(required =false)
	private I18nMessageService i18nMessageService;	
	
	private final String modulePath = "/knowledge";	

	@SearchCriteriaCls(criteriaClass = MapPaginaitionCriteria.class)
	@RequiresUser
	@RegisterRequestPermissionStrategy(
		value = modulePath+"/usergroup/page_usergroup.json",
		skipCheck = true
	)		
	@RequestMapping(value = modulePath+"/usergroup/page_usergroup.json")
	public @ResponseBody ListResult showPageAvaiableUserGroup(PaginationCriteria criteria ) throws Exception {

		Page<UserGroupListVO> page = userGroupService.pageAvailableAllList(criteria);
		
		ListResult lr = new ListResult();
		lr.setRows(page.getContent());
		lr.setResults((int) page.getTotalElements());

		return lr;
	}


	@RequiresUser
	@RegisterRequestPermissionStrategy(
		value = modulePath+"/usergroup/submit_adding.json",
		skipCheck = true
	)		
	@RequestMapping(value = modulePath+"/usergroup/submit_adding.json")
	public @ResponseBody SubmitResult accessAdd(UserGroupFormVO formView) throws Exception {
		
		// --- map to json object data ---
		SubmitResult result = new SubmitResult();
		
		try {
			
			userGroupService.addUserGroup(formView);
			result.setSuccess(true);
			//String msg = i18nMessageService.getMessage("module-client.company.add.success", null);
			result.setMsg("成功新增用户组");
			
		} catch (BusinessOperationException e) {

			e.printStackTrace();
			result.setSuccess(false);
			String msg = i18nMessageService.getMessage("module-client.company.add.error", null);
			result.setMsg(msg);

		
		}

		return result;
	}
	
	@RequiresUser
	@RegisterRequestPermissionStrategy(
		value = modulePath+"/usergroup/load.json",
		skipCheck = true
	)		
	@RequestMapping(value = modulePath+"/usergroup/load.json")
	public @ResponseBody SubmitResult accessLoad(String id) throws Exception {
		
		// --- map to json object data ---

		SubmitResult submitResult = new SubmitResult();
		if (id != null) {
			
			try {
				UserGroupFormVO userGroupFrom = userGroupService.loadUserGroup(id);
				
				submitResult.setSuccess(true);
				submitResult.setData( userGroupFrom );
				
			} catch (BusinessOperationException boe) {
				
				boe.printStackTrace();
				
			}
						
		} else {
			submitResult.setMsg(" load entity ID is null " );
		}

		return submitResult;	
	}	
	
	
	@RequiresUser
	@RegisterRequestPermissionStrategy(
		value = modulePath+"/usergroup/submit_updating.json",
		skipCheck = true
	)		
	@RequestMapping(value = modulePath+"/usergroup/submit_updating.json")
	public @ResponseBody SubmitResult accessUpdate(UserGroupFormVO formView) throws Exception {
		
		// --- map to json object data ---
		SubmitResult result = new SubmitResult();
		
		try {
			
			userGroupService.updateUserGroup(formView);
			result.setSuccess(true);
			//String msg = i18nMessageService.getMessage("module-client.company.add.success", null);
			result.setMsg("成功更新用户组");
			
		} catch (BusinessOperationException e) {

			e.printStackTrace();
			result.setSuccess(false);
			String msg = i18nMessageService.getMessage("module-client.company.add.error", null);
			result.setMsg(msg);

		
		}

		return result;
	}
	
	@RequiresUser
	@RegisterRequestPermissionStrategy(
		value = modulePath +  "/usergroup/submit_deleteall.json",
		skipCheck = true
	)		
	@RequestMapping(value = modulePath +  "/usergroup/submit_deleteall.json")
	public @ResponseBody SubmitResult accessDeleteAll(@RequestBody String[] receiveValues) throws Exception {

		// --- map to json object data ---
		SubmitResult result = new SubmitResult();
			
			// --- remove user id ---
			try {
				if ( receiveValues != null && receiveValues.length>0 ) {
					
					userGroupService.deleteUserGroupIds( receiveValues );

				}			
				result.setSuccess(true);				

				String msg = i18nMessageService.getMessage("module-admin.user.delete.success", null);
				result.setMsg(msg);
				
			} catch (BusinessOperationException se) {
				// TODO Auto-generated catch block
				se.printStackTrace();
				result.setSuccess(false);
				String msg = i18nMessageService.getMessage("module-admin.user.delete.success", null);
				result.setMsg(msg);				
			}



		return result;
	}	
	
	@RequiresUser
	@RegisterRequestPermissionStrategy(
		value = modulePath +  "/usergroup/user/submit_assign.json",
		skipCheck = true
	)		
	@RequestMapping(value = modulePath +  "/usergroup/user/submit_assign.json")
	public @ResponseBody SubmitResult accessAssignMember(String members , String id) throws Exception {

		// --- map to json object data ---
		SubmitResult result = new SubmitResult();
			
		// --- remove user id ---
		try {
			Set<String> memberIds = new LinkedHashSet<String>();
			
			
			if (members.indexOf(",") > -1 ) {
				String[] memberIdArray = members.split(",");
				
				for (String memberId : memberIdArray) {
					memberIds.add( memberId );
				}
			} else {
				memberIds.add(members );
			}
			

			userGroupService.assignMember(memberIds, id);

			result.setSuccess(true);

			String msg = i18nMessageService.getMessage(
					"module-km.admin.assignedrole.success", null);
			result.setMsg(msg);

		} catch (BusinessOperationException se) {
			// TODO Auto-generated catch block
			se.printStackTrace();
			result.setSuccess(false);
			String msg = i18nMessageService.getMessage(
					"module-admin.user.delete.success", null);
			result.setMsg(msg);
		}


		return result;
	}
	
	
	@RequiresUser
	@RegisterRequestPermissionStrategy(
		value = modulePath+"/usergroup/load_userassign.json",
		skipCheck = true
	)		
	@RequestMapping(value = modulePath+"/usergroup/load_userassign.json")
	public @ResponseBody SubmitResult accessLoadUserAssign(String id) throws Exception {
		
		// --- map to json object data ---

		SubmitResult submitResult = new SubmitResult();
		if (id != null) {
			
			try {
				UserGroupMemberFormVO userGroupMemberFrom = userGroupService.getAssignedMembersByGroupId(id);
				
				submitResult.setSuccess(true);
				submitResult.setData( userGroupMemberFrom );
				
			} catch (BusinessOperationException boe) {
				
				boe.printStackTrace();
				
			}
						
		} else {
			submitResult.setMsg(" load entity ID is null " );
		}

		return submitResult;	
	}	
		
	
}
